<template>
  <div class="tab-container">
    <div style="width:10%;float:left;" class="background">
        <el-button type="primary" size="small" @click="addOne()">添加行业分类</el-button>
        <el-input
        placeholder="输入关键字进行过滤"
        v-model="filterText"
        style="margin-top: 20px;">
        </el-input>
        <el-container>
        <el-aside width="170px">
        <el-tree      
        v-loading="treeLoading"
        element-loading-text="请给我点时间！"
        style="margin-top: 20px;"
        class="filter-tree"
        :data="items"
        :props="defaultProps"      
        node-key="id"
        default-expand-all
        :expand-on-click-node="false"
        :filter-node-method="filterNode"
        :render-content="renderContent"
        ref="tree">
        </el-tree>  
        </el-aside>
        </el-container>
        <el-dialog title="添加行业分类" :visible.sync="dialogFormVisible">
        <el-form :model="typeForm" :rules="typeRules" ref="typeForm">
            <el-form-item label="行业分类名称" label-width="">
            <el-input v-model="typeForm.typeName" auto-complete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="add()" :loading="loadingBtn">确 定</el-button>
        </div>
        </el-dialog>
    </div>
    <div style="width:86%;margin-left:170px;">
        <div class="app-container calendar-list-container">
            <div class="filter-container">
                <el-input style="width: 200px;" class="filter-item" placeholder="标题" v-model="title"></el-input>

                <el-select clearable style="width: 90px" class="filter-item" placeholder="重要性">
                </el-select>
                <el-button class="filter-item" type="primary" v-waves icon="search" @click="search()">搜索</el-button>
                <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="edit" @click="goForm()">添加</el-button>
                <el-button class="filter-item" type="primary" icon="delete" @click="del()">批量删除</el-button>

            </div>
             
            <div class="rightPostion">
                  <el-container>
                      <el-main>  
                  <el-table
                      v-loading="tableLoading"
                      element-loading-text="请给我点时间！"
                      ref="multipleTable"
                      :data="tableData"
                      tooltip-effect="dark"
                      style="width: 100%"
                      @selection-change="handleSelectionChange"
                      :row-key="getRowKeys"
                      >                
                      <el-table-column prop="id" type="selection" width="55"
                      :reserve-selection="true"></el-table-column>
                      <el-table-column type="index" label="序号" width="100"></el-table-column>
                      <el-table-column prop="question" label="题目" show-overflow-tooltip></el-table-column>
                      <el-table-column prop="classify" label="题型" width="100"></el-table-column>
                      <el-table-column prop="examType.typeName" label="分类" width="100"></el-table-column>
                      <el-table-column label="操作" width="120">                    
                          <template slot-scope="scope">
                            <el-button
                            size="mini"
                            type="primary"
                            class="filter-item"
                            @click="handleEdit(scope.$index, scope.row)"
                            >
                            编辑
                            </el-button>
                          </template>
                      </el-table-column>
                  </el-table>
                  <div class="navRight">
                    <!-- 右侧可以点击切换的列表 -->
                <transition name="el-zoom-in-right">
                    <el-aside width="350"  v-show="show">
                      <div class="scroll-bar">
                    <el-table class="borderdel"
                    :data="tableData2" 
                    style="width: 100%"
                    >
                    <el-table-column
                    type="index"
                    label="已选题"
                    width="70"
                    align="left">
                    </el-table-column>
                    <el-table-column
                      prop="question"
                      width="180"
                      >
                    </el-table-column>
                    <el-table-column
                      prop="classify"
                      width="100"
                      >
                    </el-table-column>
                    <el-table-column
                      align="right"
                      width="100"
                      >
                      <template slot-scope="scope">
                        <el-button type="danger" icon="el-icon-delete" @click="handleDelete(scope.$index, tableData2)"></el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                  </div>
                  <div class="foot">
                    <div>已选题目：10</div>
                    <div class="listBtn">
                      <el-button type="primary" @click="clearAll" class="btn-padding  btn-margin">清空</el-button>
                      <el-button type="primary" @click="onSubmit" class="btn-padding  btn-margin">生成试卷</el-button>
                    </div>
                  </div>
                  </el-aside>
                </transition>
                  <span v-bind:class="{'el-icon-d-arrow-right':show,'el-icon-d-arrow-left':!show}" @click="show=!show"></span>
                  </div>
                  </el-main>  
                  </el-container>
                  <el-pagination
                      background
                      @size-change="handleSizeChange"
                      @current-change="handleCurrentChange"
                      :current-page="currentPage"                
                      :page-sizes="[10, 20, 50, 100]"
                      :page-size="pageSize"
                      layout="total, sizes, prev, pager, next"
                      :total="pageTotal"
                      style="margin-top: 20px;">
                  </el-pagination>
            </div>
        </div>            
    </div>
  </div>
</template>
<script>
import { fetchTree, fetchDel, fetchAdd } from '@/api/profession'
import { fetchList, fetchQuestionDel } from '@/api/question'
import { isvalidateRequired } from '@/utils/validate'
import { Message } from 'element-ui'

export default {
  data() {
    const validateTypename = (rule, value, callback) => {
      if (!isvalidateRequired(value)) {
        callback(new Error('请输入行业分类名称'))
      } else {
        callback()
      }
    }
    return {
      filterText: '',
      currentPage: 1,
      pageSize: 10,
      pageTotal: 100,
      treeLoading: false,
      tableLoading: false,
      loadingBtn: false,
      dialogFormVisible: false,
      items: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      typeForm: {
        typeName: '',
        parentId: '0'
      },
      typeRules: {
        typeName: [
          { required: true, trigger: 'blur', validator: validateTypename }
        ]
      },
      nodeData: [],
      tableData: [],
      multipleSelection: [],
      treeId: '',
      title: '',
      editId: '',
      tableData2: [
      ],
      show: true,
      multipleTable: [],
      getRowKeys(row) {
        return row.id
      }
    }
  },
  created() {
    this.getList()
    this.getQuestionList()
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
      this.tableData2 = val
    },
    getQuestionList() {
      this.tableLoading = true
      fetchList(
        this.treeId,
        this.currentPage,
        this.pageSize,
        this.title
      ).then(response => {
        this.pageTotal = response.data.total
        this.tableData = response.data.data
        this.tableLoading = false
      })
    },
    search() {
      this.currentPage = 1
      this.getQuestionList(this.treeId)
    },
    del() {
      if (this.multipleSelection.length > 0) {
        this.$confirm('删除后不可恢复,确认删除？').then(response => {
          var ids = []
          this.multipleSelection.forEach(function(item) {
            ids.push(item.id)
          })
          fetchQuestionDel(ids).then(response => {
            Message.success('删除题目成功')
            this.getQuestionList()
          })
        })
      } else {
        Message.warning('请选择需要删除的题目')
      }
    },
    removeRow() {},
    getList() {
      this.treeLoading = true
      fetchTree(this.listQuery).then(response => {
        this.items = response.data.data
        this.treeLoading = false
      })
    },
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    append(data) {
      this.dialogFormVisible = true
      this.nodeData = data
    },
    add() {
      this.$refs.typeForm.validate(valid => {
        if (valid) {
          this.loadingBtn = true
          this.typeForm.parentId = this.nodeData.value
          fetchAdd(this.typeForm)
            .then(response => {
              this.typeForm.typeName = ''
              if (response.data.status === 200) {
                Message.success('添加行业分类成功')
                this.dialogFormVisible = false
                this.loadingBtn = false
                if (this.typeForm.parentId === '') {
                  this.getList()
                } else {
                  const newChild = {
                    id: response.data.data.value,
                    label: response.data.data.typeName,
                    children: []
                  }
                  if (!this.nodeData.children) {
                    this.$set(this.nodeData, 'children', [])
                  }
                  this.nodeData.children.push(newChild)
                }
              } else {
                this.dialogFormVisible = false
                this.loadingBtn = false
                Message.error('系统异常,添加行业分类失败')
              }
            })
            .catch(_ => {
              this.typeForm.typeName = ''
              Message.error('系统异常,添加行业分类失败')
            })
        } else {
          return false
        }
      })
    },
    addOne() {
      this.nodeData.value = ''
      this.dialogFormVisible = true
    },
    remove(node, data) {
      this.$confirm('删除后不可恢复,确认删除？')
        .then(response => {
          fetchDel(data.value).then(response => {
            if (response.data.status === 200) {
              Message.success('删除成功')
              const parent = node.parent
              const children = parent.data.children || parent.data
              const index = children.findIndex(d => d.value === data.value)
              children.splice(index, 1)
            } else {
              Message.error('系统异常,删除失败')
            }
          })
        })
        .catch(_ => {})
    },
    renderContent(h, { node, data, store }) {
      return (
        <div style='display:inline-block;text-algin:right;'>
          <span style='flex: 1; display: flex; align-items: center; justify-content: space-between; font-size: 14px; padding-right: 8px;'>
            <span>
              <span on-click={() => this.getQuestionListByType(data.value)} class='nodeLabel' style='text-align: left;'>
                {node.label}
              </span>
            </span>
            <span style=''>
              <el-button style='font-size: 12px;' type='text' on-click={() => this.append(data)}>添加</el-button>
              <el-button style='font-size: 12px;' type='text' on-click={() => this.remove(node, data)}>删除</el-button>
            </span>
          </span>
        </div>
      )
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getQuestionList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getQuestionList()
    },
    getQuestionListByType(val) {
      this.currentPage = 1
      this.treeId = val
      this.getQuestionList()
    },
    handleEdit(index, row) {
      this.$router.push({
        path: '/form',
        name: 'form',
        params: {
          id: row.id
        }
      })
    },
    goForm() {
      this.$router.push({
        path: '/form',
        name: 'question-form'
      })
    },
    // 右侧可以点击切换的列表js
    clearAll() {
      this.$refs.multipleTable.clearSelection()
    },
    handleDelete(index, row) { 
      row.splice(index, 1)
    }
  }
}
</script>
<style scoped>
.tab-container {
  margin: 30px;
}
.background {
  min-width: 150px;
}
.background .el-aside {
  color: #333;
  text-align: center;
  overflow: hidden;
}
.el-aside > .el-tree {
  border: 1px solid #d1dbe5;
}
.el-aside>>>.el-tree-node {
  background-color: #ffdca9;
}
.app-container {
  padding-top: 0;
}
.el-main {
  color: #333;
  text-align: left;
  border: 1px solid #dfe6ec;
  border-bottom: 0;
  padding: 0;  
  box-sizing: border-box;
  box-shadow: 0 0 4px #DADDE4;
}
.filter-container {
  padding-bottom: 4px;
}
.rightPostion {
  position: relative;
}
.rightPostion>>>.el-loading-mask {
  z-index: 99;
}
.rightPostion>>>.el-table__body-wrapper {
  overflow: hidden;
}
/* 右侧导航样式 */
.navRight {
  position: absolute;
  top: 0;
  right: 0;
  z-index: 199;
}
.navRight .el-aside {
  color: #333;
  border: 1px solid #d1dbe5;  
  position: relative;
  background-color: #ffdca9;
}
.scroll-bar {
  overflow-y: scroll;
  width: 100%;
  height: 540px;
}
.borderdel>>>.el-table {
  background-color: #ffdca9;
}
.borderdel>>>.el-table__body-wrapper {
  overflow: auto;
}
.navRight>>>.el-table__body-wrapper .is-left .cell {
  text-align: center;
}
.borderdel>>>tr {
  background-color: #ffdca9;
}
.navRight>>>.el-table__body-wrapper .cell {
  line-height: 28px;
}
.foot {
  text-align: center;
}
.foot div {
  display: inline-block;
  padding-bottom: 10px;
}
.foot div:last-child {
  margin-left: 10px;
}
.el-icon-d-arrow-right,.el-icon-d-arrow-left {
  position: absolute;
  left: 0;
  top: 300px;
  padding: 10px;
}
.el-icon-d-arrow-right:hover {
  cursor: pointer;
}
</style>
<style>
.el-aside > .el-tree > .el-tree-node .nodeLabel {
  display: inline-block;
  width: 60px;
}
.el-aside > .el-tree .el-tree-node__children .nodeLabel {
  display: inline-block;
  width: 30px;
  margin-right: 12px !important;
}
</style>
